//
// Created by yunnan xu on 2020/11/10.
//


#include "ResourceMgr.h"
#include <iostream>
#include <android/asset_manager.h>
#include <android/asset_manager_jni.h>
#include <android/storage_manager.h>
#include "EngineCore.h"


/*
 *  从 Android res 目录下面读取文件，参考：https://developer.android.com/ndk/reference/group/asset
 *
 *  其中打开文件的 mode 是如下的枚举值：
 *    AASSET_MODE_UNKNOWN = 0,
      AASSET_MODE_RANDOM = 1,
      AASSET_MODE_STREAMING = 2,
      AASSET_MODE_BUFFER = 3
 *
 * */

ResourceMgr::ResourceMgr()
{
    aAssetManager = g_engine->app->activity->assetManager;
}

long ResourceMgr::GetFileLength(const char * filename, int mode)
{
    AAsset * asset = AAssetManager_open(aAssetManager, filename, mode);

    if (!asset) {return (int) ResourceMgrErrno::FileNotExist; }

    return AAsset_getLength(asset);
}

int ResourceMgr::ReadFile(const char * filename, void * buffer, int count,  int mode)
{
    AAsset * asset = AAssetManager_open(aAssetManager, filename, mode);
    return AAsset_read(asset, buffer, count);
}